<template>
  <button @click="btnClick">点击</button>
  <div class="test">
    <div class="tab" v-useEcharts:pie="objRef"></div>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  ref,
  onMounted,
  nextTick,
  watch,
  reactive,
} from "vue";
import useEcharts from "./useEcharts";
export default defineComponent({
  name: "test",
  directives: {
    useEcharts: {
      mounted(el, bindings, vnode, preVnode) {
        const { value } = bindings;
        const { echartInstance, echartSetOption, echartResize } =
          useEcharts(el);
        console.log("mounted", bindings);
        watch(value, () => echartSetOption(value), {
          immediate: true,
          deep: true,
        });
      },
    },
  },
  setup() {
    const obj = {
      title: {
        text: "ECharts 入门示例",
      },
      tooltip: {},
      xAxis: {
        data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
      },
      yAxis: {},
      series: [
        {
          name: "销量",
          type: "bar",
          data: [5, 20, 36, 10, 10, 20],
        },
      ],
    };
    const objRef = reactive(obj);
    const btnClick = async () => {
      objRef.title.text += "666";
      objRef.series[0].data = [15, 20, 36, 120, 0, 20];
    };
    return {
      btnClick,
      objRef,
    };
  },
});
</script>
<style scoped>
.test {
  width: 100%;
  height: 100%;
  background-color: #eee;
}
.echarts {
  width: 60%;
  height: 50%;
  background-color: #ccc;
}
.tab {
  width: 50%;
  height: 50%;
  background-color: pink;
}
</style>
